<style>
</style>
<template>
  <list_url ref="list_url" :config="config">
    <template #action="{row}">
      <el-button @click="to_edit(row)" size="small">详情</el-button>
    </template>
  </list_url>
</template>
<script>
import {iframe, in_right_admin} from "@/js/tool.js";
import {_config} from "@/js/s94_tool.js";
import {ajax} from "@/js/api_tool.js";
import s94Mjs from "s94-js";
import {vue_template} from "@/js/dom_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');
    if (is_del){
      if(in_right_admin('KickbackType/restore')) btns['恢复'] = function(){ app.$refs['list_url'].restore() }
      if(in_right_admin('KickbackType/delete')) btns['彻底删除'] = function(){ app.$refs['list_url'].delete() }
    }else {
      if(in_right_admin('KickbackType/add')) btns['添加'] = function(){ iframe('kickback_type/edit') }
      if(in_right_admin('KickbackType/del')) btns['删除'] = function(){ app.$refs['list_url'].del() }
      if(in_right_admin('KickbackType/recycle')) btns['回收站'] = function(){ iframe('kickback_type/index?is_del=1') }
    }
    if (_config('this_tenant.id')) btns={};
    let type_map = _config('status_alias_map.kickback_type.type');
    let function_map = {};
    ajax({url:'/admin/kickback_type/function_config',cache:false,async:false},function (res){
      function_map = res.data.function_map;
      return false;
    })
    let forms_config = {};
    let list_cols = {
      id: {name:'id', label:'ID', width:'65px', fixed:'left', order:true},
      title: {name:'title', label:'佣金名称', width:'120px'},
      type: {name:'type', label:'类型', type:'select_one', config:type_map, width:'120px'},
      condition_info: {name:'condition_info', label:'获得佣金的条件', type:'text_more'},
    };
    if (is_del){
      list_cols['delete_time'] = {name:'delete_time', label:'删除时间', width:'165px', type:'datetime'};
    }
    list_cols['action'] = {name:'action', label:'操作', width:'80px', type:'action', fixed:'right'};

    return {
      config: {
        api: '/admin/kickback_type/'+(is_del?'recycle':'list')+'?all='+this.$get('all',''),
        title: "佣金类型"+(is_del?'(已删除)':''),
        btns: btns,
        forms_config: forms_config,
        list_cols: list_cols,
        resdata: function(list){
          s94Mjs.each(list, function (row){
            row.condition_info = app.condition_info(JSON.parse(row.condition));
          })
          return list;
        },
        limit: 20,
        p: this.$get('p', 1),
      },
      function_map: function_map,
    }
  },
  methods: {
    to_edit(row){
      let can_edit = in_right_admin('KickbackType/edit') && ~[0,row.tenant_id].indexOf(_config('this_tenant.id'))
      let param = {
        id: row.id,
      }
      if (!can_edit) param['is_info'] = 1;
      iframe('kickback_type/edit?'+s94Mjs.param_encode(param))
    },
    condition_info(condition){
      if (!this.function_map[condition.function]) return '';
      let fuc = this.function_map[condition.function];
      let data = fuc.info_param || [];
      fuc.param.forEach(function(k,i){
        data[k] = condition.param[i];
      })
      return vue_template(fuc.info, data);
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;

  }
}
</script>
